package cn.nbt.busManage.model.vo;

import cn.nbt.busManage.model.dto.BusDto;
import cn.nbt.busManage.model.dto.DriverDto;
import cn.nbt.busManage.model.dto.RouteDto;
import cn.nbt.busManage.model.entity.Driver;
import cn.nbt.busManage.model.entity.Route;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * {@code @Author}  lyq
 * {@code @Date} 6/11/2024 2:08 PM
 */

@Getter
public class BusVo implements Serializable {
    private final Integer id;
    private final String licensePlate;
    private final String model;
    private final Integer capacity;
    private final Integer status;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime lastMaintenanceDate;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime nextInspectionDate;
    private final RouteVo route;
    private final List<DriverVo> drivers ;

    public BusVo(BusDto busDto) {
        this.id = busDto.getId();
        this.licensePlate = busDto.getLicensePlate();
        this.model = busDto.getModel();
        this.capacity = busDto.getCapacity();
        this.status = busDto.getStatus();
        this.lastMaintenanceDate = busDto.getLastMaintenanceDate();
        this.nextInspectionDate = busDto.getNextInspectionDate();
        RouteDto routeDto = busDto.getRoute();
        if (routeDto != null) {
            this.route = new RouteVo(routeDto);
        } else {
            this.route = null;
        }
        List<DriverDto> driverDtoList = busDto.getDrivers();
        if (driverDtoList != null) {
            ArrayList<DriverVo> driverVos = new ArrayList<>();
            for (DriverDto driverDto : driverDtoList) {
                driverVos.add(new DriverVo(driverDto));
            }
            this.drivers = driverVos;
        }else {
            this.drivers = null;
        }
    }

    @JsonCreator
    public BusVo(@JsonProperty("id") Integer id,
                 @JsonProperty("licensePlate") String licensePlate,
                 @JsonProperty("model") String model,
                 @JsonProperty("capacity") Integer capacity,
                 @JsonProperty("status") Integer status,
                 @JsonProperty("lastMaintenanceDate") LocalDateTime lastMaintenanceDate,
                 @JsonProperty("nextInspectionDate") LocalDateTime nextInspectionDate,
                 @JsonProperty("route") RouteVo route,
                 @JsonProperty("drivers") List<DriverVo> drivers) {
        this.id = id;
        this.licensePlate = licensePlate;
        this.model = model;
        this.capacity = capacity;
        this.status = status;
        this.lastMaintenanceDate = lastMaintenanceDate;
        this.nextInspectionDate = nextInspectionDate;
        this.route = route;
        this.drivers = drivers;
    }
}
